
package com.woniu.stafforder.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.common.dto.OrderDto;
import com.woniu.common.util.PageParam;
import com.woniu.common.util.ResponseData;
import com.woniu.stafforder.dto.OrderMainDto;
import com.woniu.stafforder.entity.Order;
import com.woniu.stafforder.service.OrderService;
import com.woniu.stafforder.mapper.OrderMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author Administrator
 * @description 针对表【t_order】的数据库操作Service实现
 * @createDate 2024-05-21 18:37:47
 */
@Service
@Slf4j
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order>
        implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    private static final String REDIS_PAGE_PREFIX = "page:";
    private static final String SEPARATOR = ":";
    private static final String REDIS_ORDER_MAIN_DTO = "orderMainDto:";

    @Override
    public Boolean updateOrderStatus(Order order) {
        boolean update = lambdaUpdate().setSql("status = " + order.getStatus())
                .eq(Order::getID, order.getID())
                .update();
        return update;
    }

    @Override
    public ResponseData<?> queryOrderList(PageParam<OrderDto> pageParam) {
        Integer pageNum = pageParam.getPageNum();
        Integer pageSize = pageParam.getPageSize();

        String cacheKey = buildPagingKey(pageNum, pageSize);

        // 分页构造
        Page<OrderDto> orderPage = new Page<>(pageNum, pageSize);
        IPage<OrderDto> orderDtoIPage = orderMapper.queryOrderList(orderPage, pageParam.getParam());

        // 把订单号和订单详情id转换为字符串存到IdStr
        orderDtoIPage.getRecords().forEach(orderDto -> {
            Long orderNum = orderDto.getOrderNum();
            if (!ObjectUtil.isEmpty(orderNum)) {
                orderDto.setIdStr(orderNum.toString());
            }
            Long id = orderDto.getId();
            if (!ObjectUtil.isEmpty(id)) {
                orderDto.setChildIdStr(id.toString());
            }
        });
        return new ResponseData<>().ok(orderDtoIPage);
    }

    @Override
    public ResponseData<?> queryOrderByOrderNum(Long orderNum) {
        OrderMainDto orderMainDto = orderMapper.queryOrderByOrderNum(orderNum);
        orderMainDto.setOrderNumStr(orderMainDto.getOrderNum().toString());
        List<String> idStringList = new ArrayList<>();
        orderMainDto.getIdList().forEach(id -> {
            idStringList.add(id.toString());
        });
        orderMainDto.setIdStringList(idStringList);
        return new ResponseData<>().ok(orderMainDto);
    }


    private String buildPagingKey(int pageNum, int pageSize) {
        // 根据pageNum和pageSize构建唯一的缓存键
        return REDIS_PAGE_PREFIX + pageNum + SEPARATOR + pageSize;
    }
}


